#!/usr/bin/python
import sys,os,re
from pprint import pprint

# list of simple case-insensitive tokens to match downloaded filenames
showsToWatch=['archer','community','continuum','cutthroat.kitchen','dexter','eureka','fringe','game.of.thrones','glee','hannibal','homeland','how.i.met.your.mother','modern.family','motive','new.girl','revolution','so.you.think.you.can.dance','the.big.bang.theory','the.blacklist','top.chef']

###############################################################################################

class AvContainer:
	# the torrent download directory to search for files
	torrentBase='/storage/torrents'
	# the directory where tv shows are stored
	libraryBase='/storage/media/video/tv'
	# dictionary to corrolate the above showsToWatch to the actual library directory names
	# i.e. files in <torrentBase> that match "archer" will eventually be sent to "<libraryBase>/Archer.2009"
	destination={
              'archer':'Archer.2009',
              'community':'Community',
							'continuum':'Continuum',
							'cutthroat.kitchen':'Cutthroat.Kitchen',
              'dexter':'Dexter',
              'eureka':'Eureka.2006',
              'fringe':'Fringe',
              'game.of.thrones':'Game.of.Thrones',
              'glee':'Glee',
							'hannibal':'Hannibal',
              'homeland':'Homeland',
              'how.i.met.your.mother':'How.I.Met.Your.Mother',
              'modern.family':'Modern.Family',
							'motive':'Motive',
							'new.girl':'New.Girl',
							'revolution':'Revolution',
              'so.you.think.you.can.dance':'So.You.Think.You.Can.Dance',
              'the.big.bang.theory':'The.Big.Bang.Theory',
							'the.blacklist':'The.Blacklist',
              'top.chef':'Top.Chef'
							}

	fileMask=''
	regex=''
	allFiles=[]
	matchedFiles=[]

	def __init__(self,f):
		self.fileMask=f
		r=".*"+self.fileMask+".*s\d+e\d+"+".*\.(avi|mkv|mp4|mpg|m4v|wmv)"
		self.regex=re.compile(r,re.IGNORECASE)
		self.allFiles=[]
		self.matchedFiles=[]

	def __repr__(self):
		return self.regex

	def collect(self):
		#self.allFiles=os.listdir(self.torrentBase)
		walked=os.walk(self.torrentBase)
		for r in walked:
			for f in r[2]:
				self.allFiles.append(os.path.join(r[0],f))
		for f in filter(self.regex.match,self.allFiles):
			self.matchedFiles.append(f)

	def move(self):
		if len(self.matchedFiles) != 0:
			print('Moving:')
			for f in self.matchedFiles:
				name=os.path.basename(f)
				print("  [{src}/ >> {dst}/] {filename}".format(src=self.torrentBase,dst=os.path.join(self.libraryBase,self.destination[self.fileMask]),filename=f))
				#print(os.path.join(self.libraryBase,self.destination[self.fileMask],name.title()[:-4]+name[-4:].lower()))
				os.rename(f,os.path.join(self.libraryBase,self.destination[self.fileMask],name.title()[:-4]+name[-4:].lower()).replace(' ','.'))

###############################################################################################

if len(sys.argv) > 1:
	showsToWatch=sys.argv[1:]

for show in showsToWatch:
	avc=AvContainer(show)
	avc.collect()
	avc.move()
